layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    var laydate = layui.laydate;

    laydate.render({
        elem:'#startTime'
        ,value:new Date()
    });

    laydate.render({
        elem:'#endTime'
        ,value:new Date()
    });

    /**
     * 好想来-拣货任务管理
     */
    var HxlSortTask = {
        tableId: "hxlSortTaskTable"
    };

    /**
     * 初始化表格的列
     */
    HxlSortTask.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {align: 'center', toolbar: '#tableBar' , width: 120, title: '操作'},
            {field: 'pickOrderNumber', title: '拣货单号', width: 190},
            {field: 'deliveryDate', sort: true, title: '配送日期', width: 120},
            {field: 'deliveryWarehouse', sort: true, title: '发货仓库', width: 190},
            {field: 'recCustName', sort: true, title: '收货客户', width: 190, totalRowText: '合计：'},
            {field: 'areaName', sort: true, title: '库区', width: 120},
            {field: 'goodsCnt', sort: true, title: '商品数', width: 120, totalRow: true},
            {field: 'pickCnt', sort: true, title: '应拣数量', width: 120, totalRow: true},
            {field: 'pickVol', sort: true, title: '应拣体积(m³)', width: 120, totalRow: true},
            {field: 'pieceFlag', sort: true, title: '是否拆零', width: 120},
            {field: 'prepDt', sort: true, title: '制单时间', width: 150},
            {field: 'sorter', sort: true, title: '分拣员', width: 120},
            {field: 'receiveTime', sort: true, title: '领取时间', width: 150},
            {field: 'sortCompleteTime', sort: true, title: '分拣完成时间', width: 150},
            {field: 'agvFlag', sort: true, title: '是否AGV任务', width: 120},
            {field: 'createTime', sort: true, title: '创建时间', width: 150},
            {field: 'updateTime', sort: true, title: '修改时间', width: 150},

        ]];
    };

    /**
     * 点击查询按钮
     */
    HxlSortTask.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['deliveryWarehouse'] = $("#deliveryWarehouse").val();
        queryData['agvFlag'] = $("#agvFlag").val();
        table.reload(HxlSortTask.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    HxlSortTask.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/hxlSortTask/add';
    };

    /**
     * 导出excel按钮
     */
    HxlSortTask.exportExcel = function () {
        var checkRows = table.checkStatus(HxlSortTask.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    HxlSortTask.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/hxlSortTask/edit?pickOrderNumber=' + data.pickOrderNumber;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    HxlSortTask.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/hxlSortTask/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(HxlSortTask.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("pickOrderNumber", data.pickOrderNumber);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + HxlSortTask.tableId,
        url: Feng.ctxPath + '/hxlSortTask/list',
        page: true,
        limit: 60,
        limits:[60,120,240,360,480,1500,5000],
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        totalRow: true ,//开启合计行
        cols: HxlSortTask.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        HxlSortTask.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        HxlSortTask.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        HxlSortTask.exportExcel();
    });

    // 导入excel
    $('#btnExpimport').click(function () {
        layer.open({
            type: 2,
            title: '好想来拣货任务导入',
            area: ['1000px', '600px'],
            anim: 5,
            content: Feng.ctxPath + '/hxlSortTask/toimportExcel',
            end: function () {
                table.reload(HxlSortTask.tableId);
            }
        });
    });

    // 工具条点击事件
    table.on('tool(' + HxlSortTask.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            HxlSortTask.openEditDlg(data);
        } else if (layEvent === 'delete') {
            HxlSortTask.onDeleteItem(data);
        }
    });
});
